package com.xcw.ifelseCodereview.impl;

import com.xcw.ifelseCodereview.Expression;
import com.xcw.ifelseCodereview.Result;
import com.xcw.ifelseCodereview.Rule;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author bhx
 * @Date 2022/7/18 0:35
 * @Description TODO
 */
public class RuleEngine {
    private static List<Rule> rules = new ArrayList<>();

    static {
        rules.add(new AddRule());
    }

    public Result process(Expression expression) {
        Rule rule = rules
                .stream()
                .filter(r -> r.evaluate(expression))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("Expression does not matches any Rule"));
        return rule.getResult();
    }
}
